﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySQL.Db;

namespace Gollie.Controls
{
	public partial class VendorInvoices : AuthenticationRequiredAdministratorBaseUserControl
	{
		bool alternate = false;

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			ResultSet resultSet = Database.Queries.GetInvoices(this.UserInfo.VendorId);

			this.Populate(resultSet);
		}

		private void Populate(ResultSet resultSet)
		{
			int count = resultSet.Count;
			if (count == 0)
			{
				this.lblNoInvoices.Visible = true;

				return;
			}

			DateTime prevMonth = DateTime.Now.AddMonths(-1);

			int yearLow = resultSet.GetInt32(count-1, "year");
			int monthLow = resultSet.GetInt32(count - 1, "month");

			DateTime low = new DateTime(yearLow, monthLow, 1);
			DateTime high = new DateTime(prevMonth.Year, prevMonth.Month, 1);

			DateTime curDateTime = high;

			int curRow = 0;
			int yr = resultSet.GetInt32(curRow, "year");
			int mr = resultSet.GetInt32(curRow, "month");
			DateTime curResultSet = new DateTime(yr, mr, 1);

			while (curDateTime >= low && curRow < count)
			{
				while (curDateTime > curResultSet && curDateTime >= low)
				{
					this.CreateNoInvoiceRow(curDateTime, "Geen inkomsten");

					curDateTime = curDateTime.AddMonths(-1);
				}

				while (curResultSet >= curDateTime && curRow < count)
				{
					this.CreateInvoiceRow(resultSet, curRow, curResultSet);

					curRow++;
					if (curRow < count)
					{
						yr = resultSet.GetInt32(curRow, "year");
						mr = resultSet.GetInt32(curRow, "month");
						curResultSet = new DateTime(yr, mr, 1);
					}
				}

				curDateTime = curDateTime.AddMonths(-1);
			}
		}

		private void CreateInvoiceRow(ResultSet resultSet, int row, DateTime period)
		{
			string path = resultSet.GetString(row, "path");

			TableRow tblRow = new TableRow() { CssClass = alternate ? "alternate" : "normal" };

			if (!Helper.IsAuthorizedForInvoice(path, UserInfo.VendorId))
			{
				this.CreateNoInvoiceRow(period, "Niet geauthoriseerd");
			}
			else
			{

				tblRow.Cells.Add(CreateDownloadCell(resultSet.GetString(row, "factnr"), path));
				tblRow.Cells.Add(CreatePeriodCell(period));
				tblRow.Cells.Add(CreateFactNrCell(resultSet.GetString(row, "factnr")));
				tblRow.Cells.Add(CreateAmountCell(resultSet, row));
				tblRow.Cells.Add(CreateVatCell(resultSet, row));
				tblRow.Cells.Add(CreateSentCell(resultSet, row));

				this.tblInvoices.Rows.Add(tblRow);

				alternate = !alternate;
			}
		}

		private void CreateNoInvoiceRow(DateTime period, string reason)
		{
			TableRow tblRow = new TableRow() { CssClass = "noinvoice" };

			TableCell cell = CreateCell();
			cell.Text = reason;
			tblRow.Cells.Add(cell);

			TableCell cellPeriod = CreatePeriodCell(period);
			tblRow.Cells.Add(cellPeriod);

			for (int i = 0; i < 4; i++)
			{
				tblRow.Cells.Add(CreateCell());
			}

			this.tblInvoices.Rows.Add(tblRow);

			alternate = !alternate;
		}

		private TableCell CreateDownloadCell(string factuurNummer, string pdfPath)
		{
			string extension = System.IO.Path.GetExtension(pdfPath);
			TableCell cell = CreateCell();

			HyperLink link = new HyperLink();
			link.Target="_blank";
			link.NavigateUrl="/Download.aspx?path=" + HttpUtility.UrlEncode(pdfPath) + "&f=" + HttpUtility.UrlEncode(factuurNummer + extension);

			Image img = new Image();
			img.ImageUrl = "/Images/adobe-reader-icon.png";
			Label lbl = new Label();
			lbl.Text = "&nbsp;" + factuurNummer + extension;

			link.Controls.Add(img);
			link.Controls.Add(lbl);

			cell.Controls.Add(link);

			return cell;
		}

		private TableCell CreateFactNrCell(string factuurNummer)
		{
			TableCell cell = CreateCell();

			cell.Text = factuurNummer;

			return cell;
		}

		private TableCell CreateSentCell(ResultSet resultSet, int row)
		{
			TableCell cell = CreateCell();

			DateTime sent = resultSet.GetDateTime(row, "datesent");
			cell.Text = sent.ToString("dd-MM-yyyy");

			return cell;
		}

		private TableCell CreateAmountCell(ResultSet resultSet, int row)
		{
			TableCell cell = CreateCell();

			cell.Text = ObjectFormatter.GetCurrencyString(resultSet.GetDecimal(row, "amount"));

			return cell;
		}

		private TableCell CreateVatCell(ResultSet resultSet, int row)
		{
			TableCell cell = CreateCell();

			cell.Text = ObjectFormatter.GetCurrencyString(resultSet.GetDecimal(row, "vat"));

			return cell;
		}

		private TableCell CreatePeriodCell(DateTime period)
		{
			TableCell cell = CreateCell();

			cell.Text = period.ToString("yyyy-MMM");
			cell.CssClass += " period";

			return cell;
		}

		private TableCell CreateCell()
		{
			return new TableCell() { CssClass = "invoices" };
		}
	}
}